
#Replication data for:
#Berliner, Daniel. 2014. The Political Origins of Transparency. The Journal of Politics, 76(2): 479-491.

#setwd("YOUR DIRECTORY HERE") 

data<-read.csv("FOI_passage_finaldata.csv", header=T)

#variable definitions
#CODE country code
#YEAR year
#DEVELOPING indicator for developing countries
#FOIPASS year of FOI law passage
#turnfreq frequency of turnover
#opp1vote vote share of largest opposition party
#oppvote vote share of all opposition parties
#opp1vote_dev change over time in opp1vote
#turnfreq1 turnover frequency in categories
#FOI_REGIONAL neighborhood context of FOI passage
#FOI_IGO inter-governmental organization context of FOI passage
#polity2 Polity2 democracy index
#NEWDEM new democracy indicator
#logINGO log INGO memberships
#CORRUPTION Transparency International CPI, average over time
#logtrade log trade per GDP
#loggdpcap log GDP per capita
#AID_DEPENDENCE log foreign aid per GDP
#IMFDUMMY use of IMF credit
#FHDEM Freedom House democracy measure
#checks veto players measure from DPI
#PRESSFREEDOM Freedom House freedom of the press
#PRES presidential systems from DPI
#FDISTOCK stock of inward FDI, from UNCTAD
#GDPGRTH GDP growth
#BATTLEDEATHS battle deaths
#INJUD judicial independence from CIRI
#yrcurnt years in office of current ruling party, from DPI


options(scipen=10)

#this function from Chris Adolph simcf package, available on his website
extractdata<-function (formula, data, extra = NULL, na.rm = FALSE) 
{
    subdata <- get_all_vars(formula, data)
    if (!is.null(extra)) {
        if (is.data.frame(extra)) {
            subdata <- cbind(subdata, extra)
        }
        else {
            if (any(class(extra) == "formula")) {
                subdata <- cbind(subdata, get_all_vars(extra, 
                  data))
            }
            else {
                stop("Extra must be a dataframe or formula object")
            }
        }
    }
    if (na.rm) 
        subdata <- na.omit(subdata)
    subdata
}


#data is all years all countries
#data_d is all years developing countries only
#data_s is post90 all countries
#data_ds is post90 developing only
data_d<-data[data$DEVELOPING==1,]
data_s<-data[data$YEAR>1989,]
data_ds<-data_d[data_d$YEAR>1989,]


#set which sample to use
data_x<-data_ds


###Models in main text of paper:

#model 1: base
formula1<-FOIPASS ~ 
polity2 +
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO

#subset data for variables to be used, then create duration dependent dummy variables for number of years at risk
mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"polity2   + FOI_REGIONAL+ FOI_IGO+ NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1] #omit one as reference category
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))
}
names(selectdata) <- c(names(mdata1),ddnames)

m1<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
summary(m1)


#model 2: opp1vote
formula1<-FOIPASS ~ 
polity2 +
opp1vote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"polity2 + opp1vote + FOI_REGIONAL+ FOI_IGO+ NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))
}
names(selectdata) <- c(names(mdata1),ddnames)

m2<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
summary(m2)



#model 3: turnfreq
formula1<-FOIPASS ~ 
polity2 +
turnfreq +
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"polity2 + turnfreq + FOI_REGIONAL+ FOI_IGO+ NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))
}
names(selectdata) <- c(names(mdata1),ddnames)

m3<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
summary(m3)



#model 4: both
formula1<-FOIPASS ~ 
polity2 +
turnfreq +
opp1vote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"polity2 +opp1vote + turnfreq +FOI_REGIONAL+ FOI_IGO+ NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))
}
names(selectdata) <- c(names(mdata1),ddnames)

m4<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
summary(m4)








#### Models in online appendix

#model 5: vote share all opposition parties
formula1<-FOIPASS ~ 
polity2 +
turnfreq +
oppvote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"polity2 +oppvote+turnfreq + FOI_REGIONAL+ FOI_IGO+NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY ", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))
}
names(selectdata) <- c(names(mdata1),ddnames)

m5<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
summary(m5)



#model 6: largest opposition party change in vote share from recent average
formula1<-FOIPASS ~ 
polity2 +
turnfreq +
opp1vote_dev + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"polity2 +opp1vote_dev + turnfreq  +FOI_REGIONAL+ FOI_IGO + NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))
}
names(selectdata) <- c(names(mdata1),ddnames)

m6<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
summary(m6)



#model 7: turnfreq as factor

formula1<-FOIPASS ~ 
polity2 +
turnfreq1 +
opp1vote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"polity2 +as.factor(turnfreq1) +opp1vote+FOI_REGIONAL+ FOI_IGO+ NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY  ", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))
}
names(selectdata) <- c(names(mdata1),ddnames)

m7<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
summary(m7)



#model 8: both with FHDEM

formula1<-FOIPASS ~ 
FHDEM +
turnfreq +
opp1vote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = FALSE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"FHDEM +opp1vote + turnfreq +FOI_REGIONAL+ FOI_IGO+ NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY  ", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))
}
names(selectdata) <- c(names(mdata1),ddnames)

m8<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
summary(m8)



#model 9: veto players

formula1<-FOIPASS ~ 
polity2 +
checks+
turnfreq +
opp1vote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = FALSE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"polity2 +opp1vote + turnfreq + checks +FOI_REGIONAL+ FOI_IGO+ NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY  ", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))
}
names(selectdata) <- c(names(mdata1),ddnames)

m9<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
summary(m9)








#model 10: pressfreedom
formula1<-FOIPASS ~ 
polity2 +
PRESSFREEDOM +
turnfreq +
opp1vote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"polity2 +opp1vote + turnfreq +PRESSFREEDOM +FOI_REGIONAL+ FOI_IGO+ NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY ", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))
}
names(selectdata) <- c(names(mdata1),ddnames)

m10<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
summary(m10)






#model 11: PRES
formula1<-FOIPASS ~ 
polity2 +
turnfreq +
opp1vote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO+
PRES

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"polity2 +opp1vote+turnfreq+ PRES +FOI_REGIONAL+ FOI_IGO+NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY ", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))
}
names(selectdata) <- c(names(mdata1),ddnames)

m11<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
summary(m11)




#model 12: PRES int term
formula1<-FOIPASS ~ 
polity2 +
turnfreq +
opp1vote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO+
PRES

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"polity2 +opp1vote + turnfreq +PRES + PRES*opp1vote + PRES*turnfreq+FOI_REGIONAL+ FOI_IGO + NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY  ", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))
}
names(selectdata) <- c(names(mdata1),ddnames)

m12<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
summary(m12)



#use different sets of observations
datalist<-list(data_s, data_d, data)
reslist<-list(1:3)

for(k in 1:3){
data_x<-datalist[[k]]
formula1<-FOIPASS ~ 
polity2 +
turnfreq +
opp1vote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){
    mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]]))
}



formula1<-paste("FOIPASS","~", 
"polity2  + opp1vote +turnfreq   +FOI_REGIONAL+ FOI_IGO+NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))
}
names(selectdata) <- c(names(mdata1),ddnames)


reslist[[k]]<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
}


summary(reslist[[1]]) #model 13
summary(reslist[[2]]) #model 14
summary(reslist[[3]]) #model 15




#reset sammple to developing countries since 1990
data_x<-data_ds

#model 16: FDI
formula1<-FOIPASS ~ 
polity2 +
turnfreq +
opp1vote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO+
FDISTOCK

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"opp1vote + turnfreq +polity2+FOI_REGIONAL+ FOI_IGO+ NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY+  FDISTOCK", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))}
names(selectdata) <- c(names(mdata1),ddnames)

m16<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
summary(m16)


#model 17: econgrth
formula1<-FOIPASS ~ 
polity2 +
turnfreq +
opp1vote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO+
GDPGRTH

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"opp1vote + turnfreq +polity2+FOI_REGIONAL+ FOI_IGO+ NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY+  GDPGRTH", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))}
names(selectdata) <- c(names(mdata1),ddnames)

m17<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
summary(m17)



#model 18, dummy for negative growth
formula1<-FOIPASS ~ 
polity2 +
turnfreq +
opp1vote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO+
GDPGRTH

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"opp1vote + turnfreq +polity2+FOI_REGIONAL+ FOI_IGO+ NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY+  I(GDPGRTH<0)", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))}
names(selectdata) <- c(names(mdata1),ddnames)

m18<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
summary(m18)



#model 19: battledeaths
formula1<-FOIPASS ~ 
polity2 +
turnfreq +
opp1vote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO+
BATTLEDEATHS

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"opp1vote + turnfreq +polity2+FOI_REGIONAL+ FOI_IGO+ NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY+  I(BATTLEDEATHS>500)", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))}
names(selectdata) <- c(names(mdata1),ddnames)

m19<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
summary(m19)


#model 20: judicial independence
formula1<-FOIPASS ~ 
polity2 +
turnfreq +
opp1vote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO+
INJUD

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"opp1vote + turnfreq +polity2+FOI_REGIONAL+ FOI_IGO+ NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY+  INJUD", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))}
names(selectdata) <- c(names(mdata1),ddnames)

m20<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
summary(m20)


#model 21: years left in term
formula1<-FOIPASS ~ 
polity2 +
turnfreq +
opp1vote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO+
yrcurnt

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"opp1vote + turnfreq +polity2+FOI_REGIONAL+ FOI_IGO+ NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY+  yrcurnt", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))}
names(selectdata) <- c(names(mdata1),ddnames)

m21<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
summary(m21)




#model 22: democracy interaction
formula1<-FOIPASS ~ 
polity2 +
turnfreq +
opp1vote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"polity2 +opp1vote*polity2 + turnfreq*polity2 +FOI_REGIONAL+ FOI_IGO+ NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))
}
names(selectdata) <- c(names(mdata1),ddnames)

m22<-glm(as.formula(formula1), data=selectdata, family=binomial(link="logit"), control=glm.control(maxit=500))
summary(m22)





#Cox proportional hazards model (not actually included in appendix, but just checking)
library(survival)
library(eha)

formula1<-FOIPASS ~ 
polity2 +
turnfreq +
opp1vote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO

coxdata<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(coxdata$CODE))

#making time indicators
coxdata$yearadj<-coxdata$YEAR-( min(coxdata$YEAR)-1)
coxdata$start<-coxdata$yearadj
coxdata$stop<-coxdata$start+1


cox1 <- coxph(Surv(start, stop, FOIPASS) ~
polity2 + opp1vote +turnfreq +NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY  +FOI_REGIONAL+ FOI_IGO,
data=coxdata)

summary(cox1)



#model 23: Accelerated Failure Time (AFT) with Weibull baseline hazard
weibullaft <- aftreg(Surv(start, stop, FOIPASS) ~ polity2 + opp1vote +turnfreq +NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY  +FOI_REGIONAL+ FOI_IGO,
dist="weibull", data=coxdata)
summary(weibullaft)

#model 24: AFT w log-normal baseline hazard
lognormalaft <- aftreg(Surv(start, stop, FOIPASS) ~ polity2 + opp1vote +turnfreq +NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY  +FOI_REGIONAL+ FOI_IGO,
dist="lognormal", data=coxdata)
summary(lognormalaft)

#model 25: AFT w log-logistic baseline hazard
loglogisticaft <- aftreg(Surv(start, stop, FOIPASS) ~ polity2 + opp1vote +turnfreq +NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY  +FOI_REGIONAL+ FOI_IGO,
dist="loglogistic", data=coxdata)
summary(loglogisticaft)

#model 26: AFT w extreme-value baseline hazard
evaft <- aftreg(Surv(start, stop, FOIPASS) ~ polity2 + opp1vote +turnfreq +NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY  +FOI_REGIONAL+ FOI_IGO,
dist="ev", data=coxdata)
summary(evaft)



library(McSpatial)
Wfull1<-as.matrix(read.csv("spatial_matrix_distance.csv", row.names=1))
Wigo1<-as.matrix(read.csv("spatial_matrix_IGO.csv", row.names=1))

#Model 27, spatial probit with geographic distance matrix
formula1<-FOIPASS ~ 
polity2 +
turnfreq +
opp1vote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"polity2 +opp1vote + turnfreq + NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY+ FOI_IGO", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))
}
names(selectdata) <- c(names(mdata1),ddnames)

spp1_dist<-spprobit(as.formula(formula1), data=selectdata, wmat=Wfull1)


#Model 28, spatial probit with IGO distance matrix
formula1<-FOIPASS ~ 
polity2 +
turnfreq +
opp1vote + 
NEWDEM +
logINGO +
CORRUPTION +
logtrade +
loggdpcap +
AID_DEPENDENCE +
IMFDUMMY  +
FOI_REGIONAL+
FOI_IGO

mdata1<-extractdata(formula1, data_x, extra=data_x[,c("CODE","YEAR")], na.rm = TRUE)
uc1<-unique(as.character(mdata1$CODE))
mdata1$yearsatrisk<-rep(NA,nrow(mdata1))
for(i in 1:length(uc1)){mdata1$yearsatrisk[mdata1$CODE==uc1[i]]<- 1:(length(mdata1$yearsatrisk[mdata1$CODE==uc1[i]])) }

formula1<-paste("FOIPASS","~", 
"polity2 +opp1vote + turnfreq + NEWDEM  +logINGO+CORRUPTION +loggdpcap +logtrade + AID_DEPENDENCE +IMFDUMMY + FOI_REGIONAL", sep="")

yearsatrisk_dummies <- model.matrix(~factor(mdata1$yearsatrisk)-1)
yearsatrisk_dummies<-yearsatrisk_dummies[,-1]
selectdata <- cbind(mdata1,yearsatrisk_dummies)
ddnames <- NULL
for (i in 1:ncol(yearsatrisk_dummies)) {
  formula1 <- paste(formula1,"+dd",i,sep="")
  ddnames <- c(ddnames,paste("dd",i,sep=""))
}
names(selectdata) <- c(names(mdata1),ddnames)

spp2_igo<-spprobit(as.formula(formula1), data=selectdata, wmat=Wigo1)


